//
//  StoresCodeView.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/31.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "StoresCodeView.h"
#import "UserInfo.h"
@interface StoresCodeView ()
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UIView *backView;
@property (nonatomic, strong) UIImageView *codeImage;
@end

@implementation StoresCodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [self setup];
        [self logoQrCode];
    }
    return self;
}

- (void)setup {
    
    _userInfo = [UserInfo sharedUserInfo];
    // 265 256
    UIView *backView = [[UIView alloc] initWithFrame:(CGRectMake((self.frame.size.width - (self.frame.size.width - 56))/2, (self.frame.size.width-(self.frame.size.height - 330))/2, self.frame.size.width - 56,self.frame.size.height - 330))];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 20;
    [self addSubview:backView];
    self.backView = backView;
    
    UILabel *titleLabel = [UILabel setLabelWithText:@"您的店铺二维码" fontSize:16 color:[UIColor colorWithHexString:@"#444444"]];
    titleLabel.frame = CGRectMake((backView.frame.size.width - 100)/2, 18, 120, 16);
    [backView addSubview:titleLabel];
    
    UIButton *closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    closeBtn.frame = CGRectMake(backView.frame.size.width - 30, 18, 14, 14);
    [closeBtn setImage:[UIImage imageNamed:@"guanbi"] forState:(UIControlStateNormal)];
    [closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [backView addSubview:closeBtn];
    
    UIImageView *codeImage = [[UIImageView alloc] initWithFrame:(CGRectMake((backView.frame.size.width-(backView.frame.size.width - 102))/2, titleLabel.frame.origin.y + titleLabel.frame.size.height + 30, backView.frame.size.width - 102, backView.frame.size.width - 102))];
    [backView addSubview:codeImage];
    self.codeImage = codeImage;
    
 
}

-(void)logoQrCode{
    
    //
//    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
//    NSLog(@"%@",filters);
    
    //二维码过滤器
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //设置过滤器默认属性 (老油条)
    [qrImageFilter setDefaults];
    
    //将字符串转换成 NSdata (虽然二维码本质上是 字符串,但是这里需要转换,不转换就崩溃)
    NSData *qrImageData = [_userInfo.QR_code_img dataUsingEncoding:NSUTF8StringEncoding];
    
    //我们可以打印,看过滤器的 输入属性.这样我们才知道给谁赋值
//    NSLog(@"%@",qrImageFilter.inputKeys);
    /*
     inputMessage,        //二维码输入信息
     inputCorrectionLevel //二维码错误的等级,就是容错率
     */
    
    
    //设置过滤器的 输入值  ,KVC赋值
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    
    //取出图片
    CIImage *qrImage = [qrImageFilter outputImage];
    
    //但是图片 发现有的小 (27,27),我们需要放大..我们进去CIImage 内部看属性
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    
    //转成 UI的 类型
    UIImage *qrUIImage = [UIImage imageWithCIImage:qrImage];

    //----------------给 二维码 中间增加一个 自定义图片----------------
    //开启绘图,获取图形上下文  (上下文的大小,就是二维码的大小)
    UIGraphicsBeginImageContext(qrUIImage.size);
    
    //把二维码图片画上去. (这里是以,图形上下文,左上角为 (0,0)点)
    [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)];
    
    
    //再把小图片画上去
    UIImage *sImage = [UIImage imageNamed:@"Snip20160715_4"];
    
    //
    //    UIImageView *sImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 60, 60)];
    //    [self.view addSubview:sImageView];
    //    sImageView.image = sImage;
    //    // 类似于clip，使用masksToBounds阴影效果无效
    //    sImageView.layer.masksToBounds = YES;
    //    //图层的圆角半径
    //    sImageView.layer.cornerRadius = 50;
    
    
    
    CGFloat sImageW = 100;
    CGFloat sImageH= sImageW;
    CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5;
    CGFloat sImgaeY = (qrUIImage.size.height - sImageH) * 0.5;
    
    [sImage drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)];
    
    //获取当前画得的这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();

    
    //设置图片
    self.codeImage.image = finalyImage;
}

#pragma mark - 关闭 -
- (void)closeBtnClicked:(UIButton *)sender {
    
    [self removeFromSuperview];
}



#pragma mark - 弹出 -
- (void)showCustomStoresCodeView {
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];

}

- (void)creatShowAnimation {
    
    self.backView.layer.position = self.center;
    self.backView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }];
}
@end
